﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>533</ArticlesID><Title>Gọi XML Web Service từ ASP</Title><Body>&amp;lt;p&amp;gt;Designing the web service and calling/using it in ASP.NET is a very easy part because Microsoft .NET technology provides inherent/inbuilt support for it. But doing the same through classic ASP web application is little bit tricky...........&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Using the code&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;This article contains to major Part&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;1. .Net Web Service&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;2. ASP Web application calling the XML Web service.&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace Test_ASP_Service1

{

/// &amp;amp;lt;summary&amp;amp;gt;
/// Summary description for Service1.
/// &amp;amp;lt;/summary&amp;amp;gt;

public class Service1 : System.Web.Services.WebService
{

public Service1()
{
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();

}

#region Component Designer generated code
//Required by the Web Services Designer 

private IContainer components = null;
private void InitializeComponent()
{

}
protected override void Dispose( bool disposing )
{
    if(disposing &amp;amp;amp;&amp;amp;amp; components != null)
        {
        components.Dispose();
    }
    base.Dispose(disposing); 

}
#endregion

[WebMethod]
public string Sum(int val1,int val2)
{
    return &amp;amp;quot;The Sum of two number= &amp;amp;quot;+(val1+val2);
}
[WebMethod]
public string Subtract(int val1,int val2)
{
    return &amp;amp;quot;The Subtraction of two number= &amp;amp;quot;+ ( (val1&amp;amp;gt;val2) ? (val1-val2):(val2-val1));
}
}
}
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;The Web Service contain Two web-methos Sum() and Subtract() performing the relative operation.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Next is the ASP Web application code calling the Web Service.&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: xhtml;&amp;quot;&amp;gt;
&amp;amp;lt;html&amp;amp;gt;
&amp;amp;lt;head&amp;amp;gt;
&amp;amp;lt;title&amp;amp;gt;Calling a webservice from classic ASP&amp;amp;lt;/title&amp;amp;gt;
&amp;amp;lt;/head&amp;amp;gt;
&amp;amp;lt;body&amp;amp;gt;
&amp;amp;lt;%
If Request.ServerVariables(&amp;amp;quot;REQUEST_METHOD&amp;amp;quot;) = &amp;amp;quot;POST&amp;amp;quot; Then
Dim xmlhttp
Dim DataToSend
DataToSend=&amp;amp;quot;val1=&amp;amp;quot;&amp;amp;amp;Request.Form(&amp;amp;quot;text1&amp;amp;quot;)&amp;amp;amp;&amp;amp;quot;&amp;amp;amp;val2=&amp;amp;quot;&amp;amp;amp;Request.Form(&amp;amp;quot;text2&amp;amp;quot;)
Dim postUrl
If Request.Form.Item(&amp;amp;quot;Operation&amp;amp;quot;)=&amp;amp;quot;Sum&amp;amp;quot; Then
postUrl = &amp;amp;quot;http://localhost/Test_ASP_Service1/Service1.asmx/Sum&amp;amp;quot;
else
postUrl = &amp;amp;quot;http://localhost/Test_ASP_Service1/Service1.asmx/Subtract&amp;amp;quot;
end if
Set xmlhttp = server.Createobject(&amp;amp;quot;MSXML2.XMLHTTP&amp;amp;quot;)
xmlhttp.Open &amp;amp;quot;POST&amp;amp;quot;,postUrl,false
xmlhttp.setRequestHeader &amp;amp;quot;Content-Type&amp;amp;quot;,&amp;amp;quot;application/x-www-form-urlencoded&amp;amp;quot;
xmlhttp.send DataToSend
Response.Write DataToSend &amp;amp;amp; &amp;amp;quot;&amp;amp;lt;br&amp;amp;gt;&amp;amp;quot;
Response.Write(xmlhttp.responseText)
Else
Response.Write &amp;amp;quot;Loading for first Time&amp;amp;quot;
End If
%&amp;amp;gt;
&amp;amp;lt;FORM method=POST name=&amp;amp;quot;form1&amp;amp;quot; ID=&amp;amp;quot;Form1&amp;amp;quot;&amp;amp;gt;
Enter the two Values to perform Operation&amp;amp;lt;BR&amp;amp;gt;
&amp;amp;lt;select name=&amp;amp;quot;Operation&amp;amp;quot;&amp;amp;gt;Select Operation&amp;amp;lt;option value=&amp;amp;quot;Sum&amp;amp;quot;&amp;amp;gt;Sum&amp;amp;lt;/option&amp;amp;gt;&amp;amp;lt;option value=&amp;amp;quot;Subtraction&amp;amp;quot;&amp;amp;gt;Subtraction&amp;amp;lt;/option&amp;amp;gt;&amp;amp;lt;/select&amp;amp;gt;
&amp;amp;lt;INPUT type=&amp;amp;quot;text&amp;amp;quot; name=&amp;amp;quot;text1&amp;amp;quot; ID=&amp;amp;quot;Text1&amp;amp;quot;&amp;amp;gt;
&amp;amp;lt;INPUT type=&amp;amp;quot;text&amp;amp;quot; name=&amp;amp;quot;text2&amp;amp;quot; ID=&amp;amp;quot;Text2&amp;amp;quot;&amp;amp;gt;
&amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;BR&amp;amp;gt;
&amp;amp;lt;INPUT type=&amp;amp;quot;submit&amp;amp;quot; value=&amp;amp;quot;GO&amp;amp;quot; name=&amp;amp;quot;submit1&amp;amp;quot; ID=&amp;amp;quot;Submit1&amp;amp;quot;&amp;amp;gt;
&amp;amp;lt;/form&amp;amp;gt;
&amp;amp;lt;/body&amp;amp;gt;
&amp;amp;lt;/html&amp;amp;gt;
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Nguồn:&amp;amp;nbsp;CodeProject&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>10/16/2009 8:09:29 PM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/10/16/Goi-XML-Web-Service-tu-ASP-215</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>36</CategoryId><ArrayCatID>36|25</ArrayCatID></Article></Articles>